package com.pan.cuiniubi.utils;

import java.sql.Date;
import java.sql.Timestamp;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;

public class TypeTool {

    private static Map<String,Class> str2ClassMap = new HashMap();

    private static Function<String,Class> strToClassFunction = str -> str2ClassMap.getOrDefault(str,Object.class);

    static {
        str2ClassMap.put("char",String.class);
        str2ClassMap.put("varchar",String.class);
        str2ClassMap.put("nvarchar",String.class);
        str2ClassMap.put("text",String.class);

        str2ClassMap.put("int",Integer.class);
        str2ClassMap.put("bigint",Long.class);

        str2ClassMap.put("datetime", Timestamp.class);
        str2ClassMap.put("date", Date.class);

        str2ClassMap.put("tinyint",Short.class);
        str2ClassMap.put("smallint",Short.class);
        str2ClassMap.put("decimal",Double.class);
    }




    public static Class changeType(String type){

        Predicate<String> typeFilter = typeStr -> Pattern.matches("^"+typeStr+".*",type);
        String key = str2ClassMap.keySet().stream().filter(typeFilter).findFirst().get();
        Class aClass = strToClassFunction.apply(key);
        return aClass;
    }
}
